package com.zqh.app.manager.impl;

import com.alibaba.fastjson.JSONObject;
import com.zqh.app.dao.AllAppDataMapper;
import com.zqh.app.entity.AllAppData;
import com.zqh.app.manager.AppStatisticsManager;
import com.zqh.common.utils.DateUtils;
import com.zqh.common.utils.StatisticsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;

@Service("AppStatisticsManager")
public class AppStatisticsManagerImpl implements AppStatisticsManager {

    @Autowired
    AllAppDataMapper allAppDataMapper;

    @Override
    public void saveAllAppData() throws ParseException {
        AllAppData allAppData = new AllAppData();
        JSONObject jsonObject = StatisticsUtils.umengUappGetAllAppData();
        allAppData.setDate(DateUtils.getDateday(0));
        allAppData.setNewUsers(Integer.valueOf(jsonObject.getString("todayNewUsers")));   //新用户
        allAppData.setTotalUsers(Integer.valueOf(jsonObject.getString("totalUsers")));        //总用户
        allAppData.setLaunches(Integer.valueOf(jsonObject.getString("todayLaunches")));   //启动次数
        allAppData.setActivityUsers(Integer.valueOf(jsonObject.getString("todayActivityUsers")));   //活跃用户
        if (allAppDataMapper.getObject(allAppData) == null){
            allAppDataMapper.save(allAppData);
        }else {
            allAppDataMapper.update(allAppData);
        }
        System.out.print("ok");
    }
}
